<template>
  <el-dialog width="780px" :title="title" :visible.sync="visible">
    <el-form ref="form"  size="small" :rules="rules" :model="model" label-width="160px"  v-loading="loading">
      <el-form-item label="工号" prop="workNo">
        <el-input v-model="model.workNo"></el-input>
      </el-form-item>
      <el-form-item label="登录账号"  prop="username">
        <el-input v-model="model.username"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名"  prop="realname">
        <el-input v-model="model.realname"></el-input>
      </el-form-item>
      <el-form-item label="手机号码"  prop="phone">
        <el-input v-model="model.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱地址">
        <el-input v-model="model.email"></el-input>
      </el-form-item>
      <el-form-item label="生日">
        <el-date-picker type="date" placeholder="选择生日" v-model="model.birthday" style="width: 100%;"></el-date-picker>
      </el-form-item>
      <el-form-item label="性别">
        <el-input v-model="model.sex"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

  import Vue from 'vue'
  import { MessageBox, Message,Notification } from 'element-ui'
  import pick from 'lodash.pick'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { getAction,deleteAction,putAction,postAction } from '@/api/manage'
  import {addUser,editUser } from '@/api/api'

  export default {
    name: "UserModal",
    components: {
    },
    data () {
      return {
        title:"操作",
        visible: false,
        loading:false,
        model: {},
        url: {

        },  // 新增资产表单验证
        rules: {
          workNo: [{ required: true, message: '请输入工号', trigger: 'blur' }],
          username: [{ required: true, message: '请输入用户账号', trigger: 'blur' }],
          realname:[ { required: true, message: '请输入真实姓名', trigger: 'blur' }],
          phone:[{ required: true, message: '请输入手机号码', trigger: 'blur' }]
        },
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}

    },
    methods: {
      refresh () {

      },
      add () {
        this.refresh();
        this.edit({});
      },
      edit (record) {
        // this.resetScreenSize(); // 调用此方法,根据屏幕宽度自适应调整抽屉的宽度
        let that = this;
        if(that.$refs.form){
          that.$refs.form.resetFields();
        }
        that.model = Object.assign({}, record);
        //that.form.setFieldsValue(pick(this.model,'username','sex','realname','email','phone','activitiSync','workNo','telephone','post'))
        this.visible = true;
      },
      handleSubmit () {
        const that = this;
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true
            let doAction;
            if(!this.model.id){
              doAction = addUser;
            }else{
              doAction = editUser;
            }
            doAction(this.model).then(res =>{
              if(res.success){
                this.$message.success(res.message)
                that.$emit('ok');
              }else{
                this.$message.warning( res.message)
              }
            }).finally(() => {
              this.loading = false
              that.close();
            });
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
    }
  }
</script>

<style scoped>

</style>
